home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / comm / ntmler8b.zip / CGI.CPP < prev    next >
Text File  |  1996-09-12  |  7KB  |  259 lines

  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <string.h>
  4. #include <process.h>
  5. #include <conio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <windows.h>
  9.  
  10. #include "cgi.h"
  11.  
  12. CCGI::CCGI()
  13. {
  14. }
  15.  
  16. CCGI::~CCGI()
  17. {
  18.     delete m_method,
  19.            m_name,
  20.            m_value;
  21.  
  22. }
  23.  
  24. /////////////////////////////////////////////////////////////////////////////////
  25. //This is primarily for debugging purposes.  Allows a developer to manually set//
  26. //the form data.                                                               //
  27. /////////////////////////////////////////////////////////////////////////////////
  28. void CCGI::SetContent(char* content)
  29. {
  30.     strcpy(m_content, content);
  31. }
  32.  
  33.  
  34. /////////////////////////////////////////////////////////////////////////////////
  35. //Gets the values from the form
  36. /////////////////////////////////////////////////////////////////////////////////
  37. BOOL CCGI::Initialize(const char* method_type)
  38. {
  39.     char* clstr;
  40.     int content_length;
  41.     
  42.     m_method = getenv("REQUEST_METHOD");
  43.     //Check the Method 
  44.     if (strcmp(m_method, method_type) != 0)  
  45.         err("You must submit a <a href=index.html>form</A> to access this URL");
  46.     
  47.     //Check to make sure the browser supports this function
  48.     clstr = getenv("CONTENT_LENGTH");
  49.     if(!clstr)
  50.         err("Your browser didn't send any content.  Is it not POST capable?");
  51.     
  52.     //convert the CONTENT_LENGTH string into an integer
  53.     content_length = atoi(clstr);
  54.  
  55.     //Check the browsers content type
  56.     if(strcmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded") != 0)
  57.         err("Your browser sent the wrong content type.");
  58.     
  59.     /*
  60.      * Check for netgative or outrageously large content lengths
  61.      * An upper limit is set to make sure they don't steal extra system
  62.      * resources for no good reason
  63.      */
  64.     
  65.     if ( (content_length < 0 ) || content_length >= MAX_CONTENT_LENGTH )
  66.         err("Your browser created too much data from the form");
  67.         
  68.     fread(m_content, 1, content_length, stdin);
  69.  
  70.     //Add an additional '&' at the end so the strtok will work correctly in FindData(...)
  71.     strcat(m_content, "&");
  72.     //_strupr(m_content);
  73.     return FALSE;
  74. }
  75.  
  76. BOOL CCGI::ParamExist(char param_to_find[])
  77. {
  78.     if ( stristr(m_content, param_to_find) != NULL )
  79.         return TRUE;
  80.     return FALSE;
  81. }
  82.  
  83. //////////////////////////////////////////////////////////////////////////////////
  84. //Given 'form_variable' will return the contents of the variable.  If it doesn't//
  85. //exist, then it sends out an HTML err stating that the variable does not exist //
  86. //////////////////////////////////////////////////////////////////////////////////
  87. char* CCGI::FindData(char* form_variable, BOOL REQUIRED)
  88. {
  89.     char* strresult = NULL;
  90.     char* token;
  91.     char buf[1000];
  92.     char TempContent[MAX_CONTENT_LENGTH];
  93.  
  94.     //Parse the form_variable from the whole string(m_content).  Should now be in the 
  95.     //format of form_variable=value&form_variable=value&
  96.     
  97.     //find the form_variable requested 
  98.     strcpy(TempContent, m_content);
  99.     strresult = stristr(TempContent, form_variable); 
  100.     if (strresult == NULL)
  101.         if (!REQUIRED)
  102.             return NULL;
  103.         else
  104.         {
  105.             PrintHeader("Form Information Missing");
  106.             sprintf(buf, "Form name couldn't be found: %s", form_variable);
  107.             err(buf);
  108.         }
  109.     //Just give me all the data associated with the form_variable
  110.     //by finding the & at the end of the value
  111.     token = strtok(strresult, "&");
  112.     
  113.     //Advance the pointer to the value
  114.     strresult = strstr(token, "=");
  115.     
  116.     //Advance the pointer by one to get pasted the '='
  117.     m_value = ++strresult;
  118.     url_decode();
  119.     return m_value;
  120. }
  121.  
  122. int CCGI::is_hex(char hex)
  123. {
  124.     //*****Taken from the Netscape handbook.  ********
  125.     
  126.     //Make sure it's upper case
  127.     if (isalpha(hex))
  128.         hex = toupper(hex);
  129.  
  130.     //This just checks the character to see if it's in the two ranges
  131.     if(((hex < 'A') && (hex > 'F')) && ((hex < '0') && (hex > '9')))
  132.         return 0;
  133.     else 
  134.         return 1;
  135. }
  136.  
  137. void CCGI::url_decode()
  138. {
  139.     //*****Taken from the Netscape handbook.  ********
  140.  
  141.     //Decodes any special characters.
  142.  
  143.     //Allocate space for the new string.  We won't need more space than we 
  144.     //already have after decoding
  145.     //char *string = new char[strlen(m_value) + 1];
  146.     char *string = (char *) malloc((strlen(m_value) + 1) * sizeof(char));
  147.     //Index register fo string copy
  148.     char* enc, *dec;
  149.     //Digit is used to translate hex into character
  150.     char digit;
  151.  
  152.     //string = m_value;
  153.     
  154.     if (string == NULL)
  155.         err("The program ran out of memory.");
  156.  
  157.     //We go through the string, looking for + signs or percents
  158.     for (enc = m_value, dec = string; *enc; enc++, dec++)
  159.     {
  160.         if (*enc != '%')
  161.         {
  162.             //Plus goes to space
  163.             if (*enc == '+')
  164.             {
  165.                 *dec = ' ';
  166.             }
  167.             else 
  168.                 *dec = *enc;
  169.         }
  170.         else 
  171.         {
  172.             //Another tricky part.  First, make sure we got what we want
  173.             if ((!is_hex(enc[1])) || (!is_hex(enc[2])))
  174.                 err("Invalid escape sequence");
  175.  
  176.             //Now, advance over the % sign to the first digit and decode
  177.             //The oxdf is to turn the character into upper case
  178.             //Modification made due to that fact that the one in the handbook didn't work.
  179.             ++enc;
  180.             digit = 0;
  181.             if (*enc >= 'A')
  182.                 digit = (((*enc & 0xdf) - 'A') + 10);
  183.             else
  184.                 digit = 16*(*enc - '0');
  185.  
  186.             ++enc;
  187.             if (*enc >= 'A')
  188.                 digit += (((*enc & 0xdf) - 'A') + 10);
  189.             else
  190.                 digit += (*enc - '0');
  191.         
  192.             //Finally, transfer the digit to the new string
  193.             
  194.             *dec = digit;
  195.         }
  196.     }
  197.     *dec = '\0';
  198.     m_value = string;
  199. }
  200.  
  201. /**Generic error handler.  Since this function does not PrintHeader, make sure you call PrintHeader
  202.    before using this function, otherwise you will get a server error.  The error has to do with the
  203.    fact that you didn't specify the Content-type
  204. */
  205. void err(char errstr[])
  206. {
  207.     printf("%s\n", errstr);
  208.     PrintTrailer(NULL);
  209.     exit(0);
  210. }
  211.  
  212. void PrintHeader(const char* title)
  213. {
  214.     // Two carriage returns (\n\n) are required by the html browser.  Do not remove it 
  215.     //or you will get a web server error
  216.     printf("Content-type: text/html\n\n");
  217.     printf("<HTML>\n");
  218.     if (title != NULL)
  219.         printf("<TITLE>%s</TITLE>", title);
  220.     
  221. }
  222.  
  223. //Place for any static trailer information
  224. void PrintTrailer(const char* trailer)
  225. {
  226.     if (trailer != NULL)
  227.         printf("%s", trailer);
  228.     printf("\n</HTML>");
  229. }
  230.  
  231. ////////////////////////////////////////////////////////////////////////////////////
  232. //This performs the same function that strstr does, but this is a case-insensitive//
  233. //version.                                                                          //
  234. ////////////////////////////////////////////////////////////////////////////////////
  235. char* stristr(char* source, const char* comparewith)
  236. {
  237.     char* t1,
  238.         * t2,
  239.         * t3;
  240.     char* cw;
  241.     
  242.     t1 = _strdup(source);
  243.     t2 = t1;
  244.     t3 = source;
  245.     cw = _strdup(comparewith);
  246.  
  247.     t2 = strstr(_strupr(t1), _strupr(cw));
  248.  
  249.     if (t2 == NULL)
  250.         return NULL;
  251.     
  252.     t3 = source + (t2 - t1);
  253.     
  254.     delete t1, t2, cw;
  255.     return t3;
  256.  
  257. }
  258.  
  259.